package org.gordonframework.site.media {
	import flash.media.SoundTransform;
	import flash.media.Video;
	
	import org.gordonframework.core.events.GProcessEvent;
	import org.gordonframework.core.process.GParallel;
	import org.gordonframework.core.process.GProcess;
	import org.gordonframework.core.tween.GTween;
	import org.gordonframework.site.views.GViewPort;
	import org.gordonframework.site.views.GViewPortScaleMode;	

	/**
	 * @author sampet
	 */
	public class GVideoPlayer extends GViewPort implements GProcess {
				
		private var _loader:GVideoLoader;
		private var _buffer:GVideoBuffer;
		
		public function GVideoPlayer(width:Number = 320, height:Number = 240) {
			super(new Video(), width, height, GViewPortScaleMode.SHOW_ALL);
			
			_loader = new GVideoLoader();
			_buffer = new GVideoBuffer(this, _loader);

			video.attachNetStream(_loader.netStream);
		}
		
		public function get video():Video {
			return view as Video;
		}
		
		public function get progress():Number {
			return 0;
		}

		public function get running():Boolean {
			return false;	
		}

		public function get complete():Boolean {
			return false;	
		}

		public function get time():Number {
			return 0;
		}
		
		public function get duration():Number {
			return 0;
		}
		
		public function get loader():GProcess {
			return _loader;
		}

		public function get buffer():GProcess {
			return _buffer;
		}
		
		public function set source(value:String):void {
			_loader.url = value;
		}
		
		public function get source():String {
			return _loader.url;
		}
		
		public function set autoRewind(value:Boolean):void {
			
		}
		
		public function get autoRewind():Boolean {
			return false;
		}
		
		override public function set soundTransform(value:SoundTransform):void {
			_loader.netStream.soundTransform = value;
		}
		
		override public function get soundTransform():SoundTransform {
			return _loader.netStream.soundTransform;
		}
		
		public function start():void {
			//_loader.start();
			//_loader.netStream.resume();
		}

		public function stop():void {
			//_loader.netStream.pause();			
		}
		
		public function seek(progress:Number):void {
			_loader.netStream.seek(progress * duration);
		}
		
		override public function intro(durationMultiplier:Number=1):Number {
			if(!running) {
				start();
				view.alpha = 0;
				soundTransform = new SoundTransform(0);
			}
			//Fade in video and volume.
			new GTween(view, {alpha:1}, durationMultiplier).start();
			new GTween(this, {soundTransform:{volume:1}}, durationMultiplier).start();
			return durationMultiplier;
		}
		
		override public function outro(durationMultiplier:Number=1):Number {
			//Fade out video and volume.
			var tweens:GParallel = new GParallel();
			tweens.addProcess( new GTween(view, {alpha:0}, durationMultiplier) );
			tweens.addProcess( new GTween(this, {soundTransform:{volume:0}}, durationMultiplier) );
			tweens.addEventListener( GProcessEvent.COMPLETE, outroCompleteHandler);
			return durationMultiplier;
		}
		
		private function outroCompleteHandler(event:GProcessEvent):void {
			stop();
		}
		
	}
}